/**
 * 
 */



package com.UMLife.me.action;

import javax.microedition.midlet.MIDlet;
import com.UMLife.me.global.Global;
import com.UMLife.me.model.Registration;
import com.UMLife.me.ui.LoginUI;
import com.UMLife.me.ui.RegisterUI;
import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.CommandListener;
import de.enough.polish.ui.Display;
import de.enough.polish.ui.Displayable;





/**
 * Class Information:
 * 
 * @Author yangguang
 * @Date 2009-11-8
 * @Description
 * @MustDos
 */
public class RegisterAction implements CommandListener
{
	
	public RegisterAction(RegisterUI register)
	{
		this.init(register);
		this.addCommand(this.register);
		
	}
	
	/**
	 * 处理按钮事件
	 */
	public void commandAction(Command c, Displayable d)
	{
		if (d == this.register) // 发生在RegisterUI界面上的Command事件。
		{
			if (c == this.cmdExit)
			{
				this.exitAction();
				
			}
			if (c == this.cmdBack)
			{
				this.backAction();
				
			}
			if (c == this.cmdOk)
			{
				this.okAction();
				
			}
		}
		if (d == this.alert)// 发生在Alert上的Command事件
		{
			if (c == this.cmdCancel)
			{
				RegisterAction.this.display
						.setCurrent(RegisterAction.this.register);
			}
			if (c == this.cmdBack)
			{
				LoginUI login = Global.getLoginUI();
				this.display.setCurrent(login);
			}
			if (c == RegisterAction.this.cmdExit)
			{
				RegisterAction.this.midlet.notifyDestroyed();
			}
		}
		
	}
	
	/**
	 * 添加按钮
	 */
	private void addCommand(RegisterUI register)
	{
		register.addCommand(this.cmdOk);
		register.addCommand(this.cmdExit);
		register.addCommand(this.cmdBack);
	}
	
	/**
	 * 点击cmdBack之后的动作
	 */
	private void backAction()
	{
		if (!this.saved)
		{
			this.alert = new Alert("提示", "您还没有提交，后退？", null, AlertType.ALARM);
			this.alert.addCommand(this.cmdCancel);
			this.alert.addCommand(this.cmdBack);
			this.alert.setCommandListener(this);
			this.alert.setTimeout(Alert.FOREVER);
			this.display.setCurrent(this.alert);
		}
		else
		{
			LoginUI login = Global.getLoginUI();
			this.display.setCurrent(login);
		}
	}
	
	/**
	 * 点击cmdExit时所激发的事件
	 */
	private void exitAction()
	{
		this.alert = new Alert("提示");
		if (this.saved)
		{
			this.alert.setString("退出？");
			
		}
		if (!this.saved)
		{
			this.alert.setString("您的信息尚未提交，选择退出将不能保存信息，退出？");
			
		}
		this.alert.setTimeout(Alert.FOREVER);
		this.alert.addCommand(this.cmdExit);
		
		this.alert.addCommand(this.cmdCancel);
		this.alert.setCommandListener(this);
		this.display.setCurrent(this.alert);
		
	}
	
	/**
	 * 进行初始化
	 */
	private void init(RegisterUI register)
	{
		this.register = register;
		this.model = Global.getRegistMode();
		this.midlet = Global.getGlobalMIDlet();
		this.display = Global.getGlobalDisplay();
		this.cmdOk = new Command("提交", Command.OK, 1);
		this.cmdBack = Global.getCmdBack();
		this.cmdExit = Global.getCmdExit();
		this.cmdCancel = Global.getCmdCancel();
	}
	
	private void okAction()
	{
		
		boolean legal = this.model.registration(this.register.getName(),
				this.register.getPhone(), this.register.getPsd(), ""
						+ this.register.getAge(), null, this.register.getJob(),
				this.register.getCity());
		this.alert = new Alert("提示", "", null, AlertType.CONFIRMATION);
		if (legal)
		{
			this.alert.setString("恭喜，您已经注册成功。");
			this.saved = true;
		}
		if (!legal)
		{
			// TODO 这里应该有一些反馈信息说明为什么不能够注册，然后给予用户信息
			this.alert.setString("对不起，该用户无法注册，请重新注册");
		}
		this.alert.setTimeout(1000);
		this.display.setCurrent(this.alert, this.register);
	}
	
	private Alert			alert;
	
	private Command			cmdBack;
	
	private Command			cmdCancel;
	
	private Command			cmdExit;
	
	private Command			cmdOk;
	
	private Display			display;
	
	private MIDlet			midlet;
	
	private Registration	model;
	
	private RegisterUI		register;
	
	private boolean			saved	= false;
	
}
